<template>
  <div id="app">
    <div class="todo-container">
      <div class="todo-wrap">
        <my-header @addObj="addObj"></my-header>
        <my-list :hobbyList="hobbyList"></my-list>
        <my-footer :hobbyList="hobbyList"
                   @checkOrNot="checkOrNot"
                   @clearHasDone="clearHasDone"></my-footer>
      </div>
    </div>
  </div>
</template>

<script>
import MyHeader from "./components/MyHeader.vue"
import MyFooter from "./components/MyFooter.vue"
import MyList from "./components/MyList.vue"

export default {
  name: 'App',
  components: {
    MyHeader,
    MyFooter,
    MyList
  },
  data () {
    return {
      // hobbyList: [
      //   { id: '001', hobby: '睡觉', done: false },
      //   { id: '002', hobby: '写代码', done: true },
      //   { id: '003', hobby: '玩游戏', done: false },
      // ]
      hobbyList: JSON.parse(localStorage.getItem('hobby')) || []
    }
  },
  methods: {
    // 1.给数据给ListItem传过去
    // sendListItemData () {
    //   this.$bus.$emit('sendList', this.hobbyList)
    // },
    // 2.添加一个选项
    addObj (obj) {
      this.hobbyList.unshift(obj);
    },
    // 3.多选框的勾选和不勾选
    itemChange (item) {
      this.hobbyList.forEach(element => {
        if (element.id == item.id) {
          return element.done = !element.done
        }
      })
    },
    //4.删除选项
    deleteItem (id) {
      this.hobbyList = this.hobbyList.filter((element) => {
        return element.id !== id
      })
    },
    // 5.底部多选框全选反选事件
    checkOrNot (bool) {
      this.hobbyList.forEach((item) => {
        item.done = bool
      })
    },
    // 6.清除已完成的事件
    clearHasDone () {
      this.hobbyList = this.hobbyList.filter((item) => {
        return item.done != true
      })
    },
    // 7.编辑事件
    editItem (item, value) {
      this.hobbyList.forEach((element) => {
        if (element.id == item.id) {
          return element.hobby = value
        }
      })
    }
  },
  mounted () {
    // this.sendListItemData();
    this.$bus.$on('itemChange', this.itemChange)
    this.$bus.$on('deleteItem', this.deleteItem)
    this.$bus.$on('editItem', this.editItem)
  },
  beforeDestroy () {
    this.$bus.$off('itemChange');
    this.$bus.$off('deleteItem');
    this.$bus.$off('editItem')
  },
  watch: {
    // 开启生层监视，不然对象里面的done值变化检测不到
    hobbyList: {
      deep: true,
      handler (value) {
        // 数据永久化存储
        localStorage.setItem('hobby', JSON.stringify(value))
      }
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin-top: 60px;
}
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}
.btn-primary {
  color: black;
  background-color: lightblue;
  border: 1px solid #bd362f;
  margin-right: 10px;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
